package filtersoter

import (
	"iptv_server/v3/v2_src/utils/common"
	"strings"
)

func NewMapper() common.Mapper {
	// o := orderedmap.New[struct{}]()
	return &mapper{
		store: make(map[string]string),
	}
}

type mapper struct {
	// store *orderedmap.Orderedmap[struct{}]
	store map[string]string
}

func (m *mapper) Contains(key string) bool {
	// if _, ok := f.store.Get(key); ok {
	if _, ok := m.store[key]; ok {
		return true
	}
	return false
}

func (m *mapper) Size() int {
	return len(m.store)
}

// func (f *filter) Keys() []string {
// 	keys := make([]string, 0, len(f.store))
// 	for k := range f.store {
// 		keys = append(keys, k)
// 	}

//		return keys
//	}
func (m *mapper) Get(key string) string {
	if v, ok := m.store[key]; ok {
		return v
	}
	return key
}

func (m *mapper) put(key, value string) {
	if len(key) == 0 || len(value) == 0 {
		return
	}
	m.store[key] = value
}

func (m *mapper) Put(line string) {
	if len(line) == 0 {
		return
	}
	vs := strings.Split(line, "===")
	if len(vs) != 2 {
		return
	}
	v0 := strings.Trim(vs[0], " ")
	v1 := strings.Trim(vs[1], " \r\n")

	m.put(v0, v1)
}
